package com.ant.androiddemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

public class PopupMenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_menu);
    }

    public void popUpMenu(View view) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.setGravity(Gravity.RIGHT);
        popupMenu.inflate(R.menu.menu_popup);
        popupMenu.show();
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                int itemId = menuItem.getItemId();
                Toast.makeText(PopupMenuActivity.this,"点击了"+menuItem.getTitle(),Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }

    public void popUpChooseMenu(View view) {

        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.setGravity(Gravity.RIGHT);
        popupMenu.inflate(R.menu.menu_choose);
        popupMenu.show();
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                int itemId = menuItem.getItemId();
                Toast.makeText(PopupMenuActivity.this,"点击了"+menuItem.getTitle(),Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    public void popUpGroupMenu(View view) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.setGravity(Gravity.RIGHT);
        popupMenu.inflate(R.menu.menu_sub);
        popupMenu.show();
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                int itemId = menuItem.getItemId();
                Toast.makeText(PopupMenuActivity.this,"点击了"+menuItem.getTitle(),Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}